import java.awt.event.*;
import javax.swing.*;


public class GodDiceUI extends JFrame {

	private JPanel contentPane;
	private ImageIcon diceOne;
	private ImageIcon diceTwo;
	private ImageIcon diceThree;
	private ImageIcon diceFour;
	private ImageIcon diceFive;
	private ImageIcon diceSix;
	private JButton currentDiceOne[];
	private JButton currentDiceTwo[];
	private JButton rollDiceButton;
	private BoardUI boardUI;
	private int diceOneValue;
	private int diceTwoValue;
	private Player p;
	private BoardUI boardui;

	
	public GodDiceUI(Player play) {
		this.p=play;
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//tempo
		this.setBounds(203, 155, 450, 300);
		this.contentPane = new JPanel();
		this.contentPane.setLayout(null);
		getContentPane().add(this.contentPane);
		this.setVisible(true);
		this.setSize(400,335);
	    this.setResizable(false);
	    
	    this.diceOne = new ImageIcon("Graphics/Dice-1.jpg");
	    this.diceTwo = new ImageIcon("Graphics/Dice-2.jpg");
	    this.diceThree = new ImageIcon("Graphics/Dice-3.jpg");
	    this.diceFour = new ImageIcon("Graphics/Dice-4.jpg");
	    this.diceFive = new ImageIcon("Graphics/Dice-5.jpg");
	    this.diceSix = new ImageIcon("Graphics/Dice-6.jpg");
	    
	    this.currentDiceOne = new JButton[6];
	    this.currentDiceTwo = new JButton[6];
	    
	    this.currentDiceOne[0] = new JButton(this.diceOne);
	    this.currentDiceOne[1] = new JButton(this.diceTwo);
	    this.currentDiceOne[2] = new JButton(this.diceThree);
	    this.currentDiceOne[3] = new JButton(this.diceFour);
	    this.currentDiceOne[4] = new JButton(this.diceFive);
	    this.currentDiceOne[5] = new JButton(this.diceSix);
	    this.currentDiceTwo[0] = new JButton(this.diceOne);
	    this.currentDiceTwo[1] = new JButton(this.diceTwo);
	    this.currentDiceTwo[2] = new JButton(this.diceThree);
	    this.currentDiceTwo[3] = new JButton(this.diceFour);
	    this.currentDiceTwo[4] = new JButton(this.diceFive);
	    this.currentDiceTwo[5] = new JButton(this.diceSix);
	    this.rollDiceButton = new JButton("FAKE ROLL BABY!");
	    
	    this.diceOneValue = 1;
	    this.diceTwoValue = 1;
	    
	    for(JButton x: currentDiceOne){
	    	
	    	this.contentPane.add(x);
	    	x.setBounds(10, 20, 176, 208);
	    	x.setVisible(false);
	    	
	    	x.addActionListener(new ActionListener() {
		    	@Override
				public void actionPerformed(ActionEvent e) {
		    		
		    		if(diceOneValue == 6)
		    			diceOneValue = 1;
		    		else
		    			diceOneValue++;
		    		
		    		for(JButton x: currentDiceOne){
		    			
		    			if(x.isVisible())
		    				x.setVisible(false);
		    			
		    		}
		    		
		    		currentDiceOne[diceOneValue-1].setVisible(true);
		    	
		    	}
		    });
	    	
	    }
	    
	    for(JButton x: currentDiceTwo){
	    	
	    	this.contentPane.add(x);
	    	x.setBounds(200, 20, 176, 208);
	    	x.setVisible(false);
	    	
	    	x.addActionListener(new ActionListener() {
		    	@Override
				public void actionPerformed(ActionEvent e) {
		    		
		    		if(diceTwoValue == 6)
		    			diceTwoValue = 1;
		    		else
		    			diceTwoValue++;
		    		
		    		for(JButton x: currentDiceTwo){
		    			
		    			if(x.isVisible())
		    				x.setVisible(false);
		    			
		    		}
		    		
		    		currentDiceTwo[diceTwoValue-1].setVisible(true);
		    	
		    	}
		    });
	    	
	    }
	    
	    this.contentPane.add(this.rollDiceButton);
	    this.rollDiceButton.setBounds(126, 260, 141, 23);
	    this.rollDiceButton.setVisible(true);
	    
	    this.currentDiceOne[0].setVisible(true);
	    this.currentDiceTwo[0].setVisible(true);
	    
	    this.rollDiceButton.addActionListener(new ActionListener() {
	    	@Override
			public void actionPerformed(ActionEvent e) {
	    		
	    		exitEvent(e);
	    	
	    	}
	    });
	    
	}
	
	private void exitEvent(ActionEvent e){
		
		if(this.p.getCurrentBlockPos().getBlockNumber()!=40){
			this.movePlayer();
		}else{	
			jailDiceSequence();		
		}
		this.dispose();
	}
	
	
	public void movePlayer(){
		this.p.setLastRoll(diceOneValue+diceTwoValue);
		MovePlayerCommand movePlayer = new MovePlayerCommand();
		movePlayer.execute();
	}
	
	public void isDoubleRoll(int doubleRoll){
		
	}
	
	public void jailDiceSequence(){//bry
		
		if(this.p.getJailCounter()!=2){
			if(diceOneValue == diceTwoValue){
	
				SendOutOfJailCommand sendOutOfJailCommand = new SendOutOfJailCommand();
				sendOutOfJailCommand.execute();
	
			}else{
	
				p.setJailCounter(p.getJailCounter()+1);
	
			}
	
			NextPlayerCommand nextPlayerCommand = new NextPlayerCommand();
			nextPlayerCommand.execute();
		}else{
			SendOutOfJailCommand sendOutOfJailCommand = new SendOutOfJailCommand();
			sendOutOfJailCommand.execute();
			p.setJailCounter(0);
			movePlayer();
		}
	}
	
}
